package com.antis.olsl.data.stock;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.antis.olsl.arouterlib.DataRoutePath;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.data.R;
import com.antis.olsl.data.databinding.ActivityStockQueryBinding;
import com.antis.olsl.data.stock.store.StockStoreFragment;
import com.antis.olsl.data.stock.warehouse.StockWarehouseFragment;

/**
 * 库存查询
 *
 * @author hhh
 */
@Route(path = DataRoutePath.ACTIVITY_STOCK_QUERY)
public class StockQueryActivity extends BaseActivity<ActivityStockQueryBinding> implements View.OnClickListener {
    public static final int STOCK_STORE_TYPE = 1;
    public static final int STOCK_WAREHOUSE_TYPE = 2;
    public int currentFragmentType = -1;
    private FragmentManager fragmentManager;
    private StockStoreFragment mStockStoreFragment;
    private StockWarehouseFragment mStockWarehouseFragment;

    @Override
    protected ActivityStockQueryBinding getViewBinding() {
        return ActivityStockQueryBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initViews() {
        viewBinding.tvStore.setSelected(true);
        viewBinding.tvWarehouse.setSelected(false);
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment mainFragment = fragmentManager.findFragmentByTag("message");
        if (mainFragment != null) {
            transaction.replace(R.id.fl_content, mainFragment);
            transaction.commit();
        } else {
            loadFragment(STOCK_STORE_TYPE);
        }

        viewBinding.tvStore.setOnClickListener(this);
        viewBinding.tvWarehouse.setOnClickListener(this);
        viewBinding.imgBack.setOnClickListener(this);
    }

    private void loadFragment(int type) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (type == STOCK_STORE_TYPE) {
            if (mStockStoreFragment == null) {
                mStockStoreFragment = new StockStoreFragment();
                transaction.add(R.id.fl_content, mStockStoreFragment, "资金周转");
            } else {
                transaction.show(mStockStoreFragment);
            }
            if (null != mStockWarehouseFragment) {
                transaction.hide(mStockWarehouseFragment);
            }
            currentFragmentType = STOCK_WAREHOUSE_TYPE;
        } else {
            if (mStockWarehouseFragment == null) {
                mStockWarehouseFragment = new StockWarehouseFragment();
                transaction.add(R.id.fl_content, mStockWarehouseFragment, "成本分析");
            } else {
                transaction.show(mStockWarehouseFragment);
            }
            if (null != mStockStoreFragment) {
                transaction.hide(mStockStoreFragment);
            }
            currentFragmentType = STOCK_STORE_TYPE;
        }
        transaction.commitAllowingStateLoss();
    }

    @Override
    protected void initData() throws IllegalAccessException {

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_store) {
            viewBinding.tvStore.setSelected(true);
            viewBinding.tvWarehouse.setSelected(false);
            switchFragment(STOCK_STORE_TYPE);
        } else if (id == R.id.tv_warehouse) {
            viewBinding.tvWarehouse.setSelected(true);
            viewBinding.tvStore.setSelected(false);
            switchFragment(STOCK_WAREHOUSE_TYPE);
        } else if (id == R.id.img_back) {
            finish();
        }
    }

    private void switchFragment(int type) {
        switch (type) {
            case STOCK_STORE_TYPE:
                loadFragment(STOCK_STORE_TYPE);
                break;
            case STOCK_WAREHOUSE_TYPE:
                loadFragment(STOCK_WAREHOUSE_TYPE);
                break;
            default:
                break;
        }
    }
}